﻿using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;

namespace Sidvall.UI.Uwp
{
    public static class ControlManager
    {
        #region Public Members

        #region GetBindingExpression

        public static BindingExpression GetBindingExpression(FrameworkElement item)
        {
            var bindingExpression = GetBindingExpression(item as TextBox, TextBox.TextProperty);
            if (bindingExpression != null)
                return bindingExpression;
            bindingExpression = GetBindingExpression(item as PasswordBox, PasswordBox.PasswordProperty);
            if (bindingExpression != null)
                return bindingExpression;
            bindingExpression = GetBindingExpression(item as Windows.UI.Xaml.Controls.Primitives.Selector, Windows.UI.Xaml.Controls.Primitives.Selector.SelectedItemProperty);
            if (bindingExpression != null)
                return bindingExpression;

            return null;
        }
        public static BindingExpression GetBindingExpression(Windows.UI.Xaml.Controls.Primitives.Selector item, DependencyProperty dp)
        {
            if (item != null)
                return item.GetBindingExpression(dp);
            return null;
        }
        public static BindingExpression GetBindingExpression(PasswordBox item, DependencyProperty dp)
        {
            if (item != null)
                return item.GetBindingExpression(dp);
            return null;
        }
        public static BindingExpression GetBindingExpression(TextBox item, DependencyProperty dp)
        {
            if (item != null)
                return item.GetBindingExpression(dp);
            return null;
        }

        #endregion
        #region GetFocusedBindingExpression

        public static BindingExpression GetFocusedBindingExpression()
        {
            var focusedElement = FocusManager.GetFocusedElement();
            return GetBindingExpression(focusedElement as FrameworkElement);
        }

        #endregion
        #region SetHeight

        public static void SetHeight(FrameworkElement element, double desiredHeight, double? maxHeight)
        {
            if ((maxHeight != null) && (maxHeight.Value < desiredHeight))
                desiredHeight = maxHeight.Value;
            element.MinHeight = desiredHeight;
            element.MaxHeight = desiredHeight;
        }

        #endregion
        #region SetWidth

        public static void SetWidth(FrameworkElement element, double desiredWidth, double? maxWidth)
        {
            if ((maxWidth != null) && (maxWidth.Value < desiredWidth))
                desiredWidth = maxWidth.Value;
            element.MinWidth = desiredWidth;
            element.MaxWidth = desiredWidth;
        }

        #endregion
        #region UpdateBindingExpression

        public static void UpdateBindingExpression(FrameworkElement item)
        {
            var binding = GetBindingExpression(item);
            if (binding == null)
                return;
            binding.UpdateSource();
        }

        #endregion
        #region UpdateFocusedBindingExpression

        public static void UpdateFocusedBindingExpression()
        {
            var binding = GetFocusedBindingExpression();
            if (binding == null)
                return;
            binding.UpdateSource();
        }

        #endregion

        #endregion
    }
}
